//
//  EditPassagerVC.swift
//  旅航ios
//
//  Created by tulip on 2021/5/10.
//

import UIKit

class EditPassagerVC: CommonVC {
    var id:String!
    var sendDic:Dictionary<String,Any>!
    var idTypeNum=1
    var isEdit=false
    @IBOutlet weak var idType: UIButton!
    var idTypeView:IdTypePickView!
    @IBOutlet weak var phoneText: UITextField!
    @IBOutlet weak var idNum: UITextField!
    @IBOutlet weak var nameText: UITextField!
    @IBOutlet weak var addBtn: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
//        let head=BackHeaderView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: TopSpaceHeight))
//        self.view.addSubview(head)
        
        
        
        addBtn.layer.cornerRadius=5
        
        if isEdit {
            head.title.text="编辑乘机人"
            addBtn.setTitle("修改", for: .normal)
            
            transportTextData(dic: sendDic)
            
        }else{
            head.title.text="增加乘机人"
        }
        
    }


    

    @IBAction func add(_ sender: Any) {
        if nameText.text == ""{
            
            YProgressHUD.showToast("请输入姓名")
        }
       else if idNum.text == ""{
            
            YProgressHUD.showToast("请输入证件号")
        }
       else if phoneText.text == ""{
            
            YProgressHUD.showToast("请输入手机号")
       }else{
        if isEdit {
            let dic=["token":TOKEN,"doument_type":idTypeNum,"mobile":phoneText.text ?? "","name":nameText.text ?? "","idcard":idNum.text ?? "","id":id] as [String : Any]
            Network.request(.editPassager(dic)) { (JSON) in
                YProgressHUD.showToast(JSON["msg"].string)
                self.navigationController?.popViewController(animated: true)
            } failure: { (err) in
                
            }
        }else{
            let dic=["token":TOKEN,"doument_type":idTypeNum,"mobile":phoneText.text ?? "","name":nameText.text ?? "","idcard":idNum.text ?? ""] as [String : Any]
            Network.request(.addPassager(dic)) { (JSON) in
                YProgressHUD.showToast(JSON["msg"].string)
                self.navigationController?.popViewController(animated: true)
            } failure: { (err) in
                
            }
        }

       }
        
    }
    
    @IBAction func selectIDType(_ sender: Any) {
        if idTypeView == nil {
            idTypeView=IdTypePickView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
            idTypeView.myidType={[weak self](turple) in
                                
                self?.idTypeNum=turple.0
                self?.idType.setTitle(turple.1, for: .normal)
            }
        }
        
        self.view.addSubview(idTypeView)
    }
    
    func transportTextData(dic:Dictionary<String,Any>){
        self.idTypeNum = dic["idTypeNum"] as! Int
        self.nameText.text = dic["name"]  as? String
        self.phoneText.text = dic["phone"]  as? String
        self.idNum.text = dic["idNum"]  as? String
        self.id=dic["id"] as? String
    }
    
    
}
